<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Api\Controller;

class FriendController extends BaseController {
    /*
     * 存储消息
     */

    private $friendId = "friendId";
    private $preKey = "friend";

    public function saveMsg() {
        $result = [];
        $this->_redis->set("aaa", json_encode(I()));
        $msg = I('msg', '', 'string');
        if (strlen($msg) <= 0) {
            $result = ['code' => "10001", "error_msg" => "不能为空"];
            exit(json_encode($result));
        }
        $id = $this->_redis->incr($this->friendId);
        $param['msg'] = $msg;
        $param['id'] = $id;
        $jsonMsg = json_encode($param);
        $msgKey = $this->preKey . 'msg';
        $return = $this->_redis->hset($msgKey, $id, $jsonMsg);
        if ($return) {
            $result = ['code' => "10000", "sucess_msg" => "保存成功"];
        } else {
            $result = ['code' => "10002", "sucess_msg" => "保存失败"];
        }
        exit(json_encode($result));
    }

    public function getMsg() {
        $id = I('id', '0', 'int');
        $msgKey = $this->preKey . 'msg';
        $result = [];
        if ($id > 0) {
            $result['code'] = "10000";
            $result['data'] = json_decode($this->_redis->hget($msgKey, $id), true);
        } else {
            $list = $this->_redis->hGetAll($msgKey);
            $result['code'] = "10000";
            $result['data'] = $list;
        }
        exit(json_encode($result));
    }

}
